package com.tongtong.stsuserapi.service.base.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tongtong.stscommon.enumration.Message;
import com.tongtong.stscommon.enumration.Status;
import com.tongtong.stscommon.exception.STSResourcesOperationException;
import com.tongtong.stscommon.message.STSMessage;
import com.tongtong.stscommon.message.STSSuccessMessage;
import com.tongtong.stscommon.utils.checker.ObjectChecker;
import com.tongtong.stsuserapi.dao.ItemDao;
import com.tongtong.stsuserapi.dao.ShoppingCartDao;
import com.tongtong.stsuserapi.pojo.Item;
import com.tongtong.stsuserapi.pojo.ShoppingCart;
import com.tongtong.stsuserapi.service.base.ShoppingCartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.util.List;
import java.util.Objects;

@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {

	@Autowired
	ShoppingCartDao shoppingCartDao;

	@Autowired
	ItemDao itemDao;

	@Override
	public STSMessage addItem(Long userid, Long itemId, Integer quantity) {

		Item item = itemDao.selectById(itemId);
		// 检测指定的商品是否存在
		ObjectChecker.ObjectsFromDBNullCheck(item);

		if(quantity > item.getQuantity()-item.getOrderedQuantity()) {
			throw new STSResourcesOperationException(Status.AddResourcesError,Message.QuantityExceeded);
		}
		// 新建一个购物车商品
		ShoppingCart shoppingCart = new ShoppingCart();
		shoppingCart.setItemId(itemId);
		shoppingCart.setUserId(userid);
		shoppingCart.setItemCount(quantity);
		shoppingCart.setCreateTime(new Timestamp(System.currentTimeMillis()));

		try	{
			shoppingCartDao.insert(shoppingCart);
		} catch (Exception e) {
			throw new STSResourcesOperationException(Status.AddResourcesError,e.getMessage());
		}

		return STSSuccessMessage.from(Status.OK,Message.OK);
	}

	@Override
	public STSMessage deleteItem(Long userid, Long cartId) {

		// 判断是否存在
		ShoppingCart shoppingCart = shoppingCartDao.selectById(cartId);
		ObjectChecker.ObjectsFromDBNullCheck(shoppingCart);

		// 判断操作的购物车是否属于当前用户
		if(!shoppingCart.getUserId().equals(userid)) {
			throw new STSResourcesOperationException(Status.AccessResourcesError,Message.AccessResourcesError);
		}

		try	{
			shoppingCartDao.deleteById(cartId);
		} catch (Exception e) {
			throw new STSResourcesOperationException(Status.DeleteResourcesError,e.getMessage());
		}

		return STSSuccessMessage.from(Status.OK,Message.OK);
	}

	@Override
	public STSMessage updateItem(Long userid, Long cartId, Integer quantity) {

		ShoppingCart shoppingCart = shoppingCartDao.selectById(cartId);
		ObjectChecker.ObjectsFromDBNullCheck(shoppingCart);

		Item item = itemDao.selectById((shoppingCart.getItemId()));
		ObjectChecker.ObjectsFromDBNullCheck(item);

		// 检测是否超过可预定数量
		if(quantity > item.getQuantity()-item.getOrderedQuantity()) {
			throw new STSResourcesOperationException(Status.AddResourcesError,Message.QuantityExceeded);
		}
		// 判断操作的商品是否属于当前用户
		if(!shoppingCart.getUserId().equals(userid)) {
			throw new STSResourcesOperationException(Status.AccessResourcesError,Message.AccessResourcesError);
		}

		shoppingCart.setItemCount(quantity);

		try	{
			shoppingCartDao.updateById(shoppingCart);
		} catch (Exception e) {
			throw new STSResourcesOperationException(Status.UpdateResourcesError,e.getMessage());
		}

		return STSSuccessMessage.from(Status.OK,Message.OK);
	}

	@Override
	public STSMessage getItems(Long userid, Integer page, Integer pageSize) {

		Page<ShoppingCart> pageInfo = new Page(page,pageSize);
		LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<>();
		lqw.eq(ShoppingCart::getUserId, userid);
		lqw.orderByDesc(ShoppingCart::getCreateTime);
		Page<ShoppingCart> cartPage = shoppingCartDao.selectPage(pageInfo, lqw);
		List<ShoppingCart> list = cartPage.getRecords();

		return STSSuccessMessage.from(list);
	}

}
